import request from '@/utils/request'
import store from '@/store'

// 登录方法
export function login(regionName, username, password) {
  const data = {
    regionName,
    username,
    password
  }
  return request({
    url: '/console/login',
    method: 'post',
    data: data
  })
}

// 退出方法
export function logout() {
  return request({
    url: '/console/logout',
    method: 'post'
  })
}

// 获取管理节点列表
export function getMasterList() {
  return request({
    url: '/console/getMasterList',
    method: 'get'
  })
}

// 启动管理节点
export function startMaster(index) {
  const data = { index };
  return request({
    url: '/console/startMaster',
    method: 'post',
    data: data
  })
}

// 停止管理节点
export function stopMaster(index) {
  const data = { index };
  return request({
    url: '/console/stopMaster',
    method: 'post',
    data: data
  })
}

// 重启控制台
export function rebootConsole() {
  return request({
    url: '/console/rebootConsole',
    method: 'post'
  })
}

// 查询菜单列表
export function listMenu() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/getMenuList',
    method: 'get',
    params: query
  })
}

// 查询菜单
export function getMenu(menuId) {
  const query = { regionName: store.getters.regionName, menuId: menuId };
  return request({
    url: '/console/getMenuById',
    method: 'get',
    params: query
  })
}

// 新增菜单
export function addMenu(data) {
  data.regionName = store.getters.regionName;
  return request({
    url: '/console/addMenu',
    method: 'post',
    data: data
  })
}

// 修改菜单
export function updateMenu(data) {
  data.regionName = store.getters.regionName;
  return request({
    url: '/console/updateMenu',
    method: 'post',
    data: data
  })
}

// 删除菜单
export function delMenu(menuId) {
  const query = { menuId: menuId, regionName: store.getters.regionName }
  return request({
    url: '/console/deleteMenu',
    method: 'get',
    params: query
  })
}

// 获取主机列表
export function getHostList() {
  return request({
    url: '/console/getHostList',
    method: 'get'
  })
}

// 启动代理节点
export function startProxy(host) {
  const query = { host: host }
  return request({
    url: '/console/startProxy',
    method: 'get',
    params: query
  })
}

// 停止代理节点
export function stopProxy(host) {
  const query = { host: host }
  return request({
    url: '/console/stopProxy',
    method: 'get',
    params: query
  })
}

// 获取系统配置文件列表
export function getConfList() {
  return request({
    url: '/console/getConfList',
    method: 'get'
  })
}

// 获取worker配置列表
export function getWorkerConfList(regionName, deployName) {
  return request({
    url: '/console/getWorkerConfList',
    method: 'get',
    params: { regionName, deployName },
  })
}

// 获取新建文件的目录
export function getConfRoot(sys, regionName, deployName) {
  return request({
    url: '/console/getConfRoot',
    method: 'get',
    params: { sys, regionName, deployName },
  })
}

// 获取配置内容
export function getConfContent(path) {
  const data = { path }
  return request({
    url: '/console/getConfContent',
    method: 'post',
    data: data
  })
}

// 保存配置内容
export function saveConfContent(path, content) {
  const data = { path, content }
  return request({
    url: '/console/saveConfContent',
    method: 'post',
    data: data
  })
}

// 新建一个配置文件
export function newConf(sys, regionName, deployName, fileName) {
  const data = { sys, regionName, deployName, fileName }
  return request({
    url: '/console/newConf',
    method: 'post',
    data: data
  })
}

// 删除配置文件
export function deleteConf(path) {
  const data = { path }
  return request({
    url: '/console/deleteConf',
    method: 'post',
    data: data
  })
}

// 获取菜单树
export function getMenuTree() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/getMenuTree',
    method: 'get',
    params: query
  })
}

// 获取worker列表
export function getWorkerList() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/getWorkerList',
    method: 'get',
    params: query
  })
}

// 通过菜单路径获取worker列表
export function getWorkerListByPath(path) {
  const query = { regionName: store.getters.regionName, path: path };
  return request({
    url: '/console/getWorkerListByPath',
    method: 'get',
    params: query
  })
}

// 启动进程
export function startWorker(regionName, deployName, groupName, workerName, index) {
  const data = { regionName, deployName, groupName, workerName, index }
  return request({
    url: '/console/startWorker',
    method: 'post',
    data: data
  })
}

// 停止进程
export function stopWorker(regionName, deployName, groupName, workerName, index, force) {
  const data = { regionName, deployName, groupName, workerName, index, force }
  return request({
    url: '/console/stopWorker',
    method: 'post',
    data: data
  })
}

// 获取上传url
export function getUploadUrl() {
  return process.env.VUE_APP_BASE_API + '/console/upload/';
}

// 获取部署包列表
export function getDeployList() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/getDeployList',
    method: 'get',
    params: query
  })
}

// 重新加载配置
export function loadConf() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/loadConf',
    method: 'get',
    params: query
  })
}

// 移除安装包
export function removeDeploy(deployName) {
  const query = { regionName: store.getters.regionName, deployName: deployName };
  return request({
    url: '/console/removeDeploy',
    method: 'get',
    params: query
  })
}

// 获取node列表
export function getNodeInfos() {
  return request({
    url: '/console/getNodeInfos',
    method: 'get'
  })
}

// 获取日志文件列表
export function getHostLogList(host, dir) {
  const query = { regionName: store.getters.regionName, host: host, dir: dir };
  return request({
    url: '/console/getHostLogList',
    method: 'get',
    params: query
  })
}

// 获取日志内容
export function getLogContent(host, path) {
  const query = { host, path };
  return request({
    url: '/console/getLogContent',
    method: 'get',
    params: query
  })
}

// 清空日志
export function clearLogContent(host, path) {
  const data = { host, path }
  return request({
    url: '/console/clearLogContent',
    method: 'post',
    data: data
  })
}

// 下载日志
export function getDownUrl(host, path) {
  return process.env.VUE_APP_BASE_API + '/console/downLogContent?host=' + encodeURIComponent(host) + '&path=' + encodeURIComponent(path);
}

// 获取job列表
export function getJobList() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/getJobList',
    method: 'get',
    params: query
  })
}

// 获取job列表
export function getJobListByPath(path) {
  const query = { regionName: store.getters.regionName, path: path };
  return request({
    url: '/console/getJobListByPath',
    method: 'get',
    params: query
  })
}

// 启动job
export function startJob(regionName, deployName, groupName, jobName) {
  const data = { regionName, deployName, groupName, jobName }
  return request({
    url: '/console/startJob',
    method: 'post',
    data: data
  })
}

// 停止job
export function stopJob(regionName, deployName, groupName, jobName) {
  const data = { regionName, deployName, groupName, jobName }
  return request({
    url: '/console/stopJob',
    method: 'post',
    data: data
  })
}

// 运行job
export function runJob(regionName, deployName, groupName, jobName, time) {
  const data = { regionName, deployName, groupName, jobName, time }
  return request({
    url: '/console/runJob',
    method: 'post',
    data: data
  })
}

// 杀死job
export function killJob(regionName, deployName, groupName, jobName) {
  const data = { regionName, deployName, groupName, jobName }
  return request({
    url: '/console/killJob',
    method: 'post',
    data: data
  })
}

// 获取JVM列表
export function getJvmList(host) {
  const query = { host };
  return request({
    url: '/console/getJvmList',
    method: 'get',
    params: query
  })
}

// 下载jvm命令文件
export function getDownCmdJvmInfoUrl(host, type, pid) {
  return process.env.VUE_APP_BASE_API + '/console/downCmdJvmInfo?host=' + encodeURIComponent(host)
    + '&type=' + encodeURIComponent(type) + '&pid=' + encodeURIComponent(pid);
}

// 获取目录树
export function fileTreeNode(host, path) {
  const query = { host, path };
  return request({
    url: '/console/fileTreeNode',
    method: 'get',
    params: query
  })
}

// 获取文件列表
export function fileInfoList(host, path) {
  const query = { host, path };
  return request({
    url: '/console/fileInfoList',
    method: 'get',
    params: query
  })
}

// 获取上传url
export function getUploadFileUrl() {
  return process.env.VUE_APP_BASE_API + '/console/uploadFile/';
}


// 删除文件
export function deleteFile(host, path) {
  const data = { host, path }
  return request({
    url: '/console/deleteFile',
    method: 'post',
    data: data
  })
}

// 获取系统监控信息
export function getAllSystemInfo() {
  return request({
    url: '/console/getAllSystemInfo',
    method: 'get'
  })
}

// 获取系统告警信息
export function getAllAlarmInfo() {
  return request({
    url: '/console/getAllAlarmInfo',
    method: 'get'
  })
}

// 删除告警信息
export function removeAlarmInfo(type) {
  const query = { type };
  return request({
    url: '/console/removeAlarmInfo',
    method: 'get',
    params: query
  })
}

// 获取程序包信息
export function getPackageInfo() {
  return request({
    url: '/console/getPackageInfo',
    method: 'get'
  })
}

// 获取安装包信息
export function getDeployInfo() {
  const query = { regionName: store.getters.regionName };
  return request({
    url: '/console/getDeployInfo',
    method: 'get',
    params: query
  })
}

// 创建部署目录
export function createDeployDir(deployName) {
  const query = { regionName: store.getters.regionName, deployName: deployName };
  return request({
    url: '/console/createDeployDir',
    method: 'get',
    params: query
  })
}

// 获取部署包版本列表
export function getDeployVersionList(deployName) {
  const query = { regionName: store.getters.regionName, deployName: deployName };
  return request({
    url: '/console/getDeployVersionList',
    method: 'get',
    params: query
  })
}

//  改变部署包属性
export function changeDeployProp(deployName, key, value) {
  const query = { regionName: store.getters.regionName, deployName: deployName, key: key, value: value };
  return request({
    url: '/console/changeDeployProp',
    method: 'get',
    params: query
  })
}

//  改变部署包属性
export function getGatewayProxyInfo(gatewayGroupName, gatewayWorkerName, gatewayIndex, gatewayDeployName) {
  const gatewayRegionName = store.getters.regionName;
  const query = { gatewayGroupName, gatewayWorkerName, gatewayIndex, gatewayRegionName, gatewayDeployName };
  return request({
    url: '/console/getGatewayProxyInfo',
    method: 'get',
    params: query
  })
}

// 下载文件
export function getDownFileUrl(path) {
  return process.env.VUE_APP_BASE_API + '/console/downFile?path=' + encodeURIComponent(path);
}

//  获取文件内容
export function getContent(path) {
  const query = { path };
  return request({
    url: '/console/getContent',
    method: 'get',
    params: query
  })
}